export default {
  getLocation() {
    // #ifdef H5
    if (Date) {
      return {
        lat: 30.280848,
        lng: 120.049525,
      };
    }
    // #endif
    return new Promise((resolve, reject) => {
      console.log("pos");
      uni.getLocation({
        type: "gcj02",
        success({ latitude, longitude }) {
          resolve({
            lat: latitude,
            lng: longitude,
          });
        },
        fail: (err) => {
          reject(err);
        },
      });
    });
  },
  getDistance(p1, p2) {
    let km = this.calcDistance(p1, p2);
    if (!km) return "";
    if (km < 1) return (km * 1e3).toFixed(0) + "m";
    return km.toFixed(1) + "km";
  },
  calcDistance(coord1, coord2) {
    if (!coord1 || !coord2) {
      return null;
    }
    const earthRadius = 6371; // Earth radius in kilometers

    const lat1 = (coord1.lat * Math.PI) / 180;
    const lat2 = (coord2.lat * Math.PI) / 180;
    const deltaLat = ((coord2.lat - coord1.lat) * Math.PI) / 180;
    const deltaLon = ((coord2.lng - coord1.lng) * Math.PI) / 180;

    const a =
      Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
      Math.cos(lat1) *
        Math.cos(lat2) *
        Math.sin(deltaLon / 2) *
        Math.sin(deltaLon / 2);
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    return earthRadius * c; // Distance in kilometers
  },
  openLocation(info, scale = 14) {
    const { name, addr, lat, lng } = info;
    uni.openLocation({
      name,
      longitude: lng * 1, // 经度
      latitude: lat * 1, // 纬度
      scale,
      address: addr,
    });
  },
};
